-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[11.x] Ensure batched jobs are actually batchable #54442
[11.x] Ensure batched jobs are actually batchable #54442
Conversation
Looks like I missed a few things in this implementation. I'm looking into it... Converted the PR to draft, in the meantime. Edit: I think we're good for review, now. |
src/Illuminate/Bus/PendingBatch.php
Outdated
return; | ||
} | ||
|
||
if (! in_array(Batchable::class, class_uses_recursive($job))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it wouldn't be useful to cache the job's class so you don't have to call class_uses_recursive for multiple instances of the same class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it's not possible with class_uses_recursive
, this could be taken one step further, by implementing something similar to class_uses_recursive
: Caching all classes in the recursive chain 🤔
There might be cases, even though, it might be rare, where the trait isn't even used for some reason, but the functionality is implemented. This wouldn't work anymore. This could possibly be avoided by using an interface 🤔 |
This PR has unnecessary doc block changes. |
Reverted those automated changes. |
Although the documentation for batch jobs clearly states that batched jobs should use the
Batchable
trait, there's not much that alerts or prevents the dev from wrongly trying to batch jobs that don't implement it, resulting in runtime problems. Even if we use theassertBatched
test, tests pass even with invalid jobs supplied. I was a victim of this. (I know, it might also be a skill issue from my side, for forgetting to use the trait...)This PR adds a check to batched jobs to ensure that the jobs use the required
Batchable
trait, throwing aRuntimeException
if a job is found in a given list when callingBus::batch
or if supplied to theadd
method of aPendingBatch
instance.